home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / server / conf.c next >
Encoding:
C/C++ Source or Header  |  1993-05-02  |  5.5 KB  |  187 lines

  1. #include "tweak.h"
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include "server_def.h"
  5. #include "s_extern.h"
  6. #include "co_extern.h"
  7.  
  8. static int conf_read = 0;
  9.  
  10. int daemonize = 0;
  11. int always_use_cache_dir = 0;
  12. int read_only = 0;
  13. int udp_port = 0;
  14. int dbug = 0;
  15. int run_uid = 0;
  16. int priv_mode = 0;
  17. int no_unnamed = 0;
  18. int logging = 0;
  19. int maxthcallowed = 0;
  20.  
  21. char *logname = NULL;
  22. char *home_dir = NULL;
  23. char *dir_cache_dir = NULL;
  24. char *readme_file = NULL;
  25.  
  26. extern int inetd_mode;
  27. extern IPrange **iptab;
  28. extern unsigned int ipcnt, iptot;
  29. extern char *config_file ;
  30.  
  31. extern char *strdup();
  32. extern IPrange *parse_ipline();
  33.  
  34. static void log_set PROTO2(int, flag, int, neg)
  35. {
  36.   if(neg) logging &= ~flag;
  37.   else logging |= flag;
  38. }
  39.  
  40. void static read_configuration PROTO1(char *, name)
  41. {
  42.   FILE *fp;
  43.   char buf[1024], *p, *q;
  44.  
  45.   fp = fopen(name,"r");
  46.   if(!fp) {
  47.     fprintf(stderr, "Unable to open configuration file: %s. Exiting.\n", name);
  48.     exit(-1);
  49.   }
  50.   while(fgets(buf, sizeof(buf), fp)) {
  51.     if(buf[0] == '\n' || buf[0] == '#') continue;
  52.     buf[strlen(buf)-1] = '\0'; /* strip off the newline */
  53.     p = buf; while(*p && isspace(*p)) p++;
  54.     q = p; while(*q && !isspace(*q)) q++;
  55.     if(!*p || !*q) {
  56.       fprintf(stderr,"Bogus line in configuration file: %s. Exiting.\n",name);
  57.       exit(-1);
  58.     }
  59.     *q = '\0'; q++;
  60.     if(strcasecmp(p, "conf") == 0) {
  61.       if(conf_read) {
  62.     fprintf(stderr, "No recursion of conf commands allowed. Skipping.\n");
  63.     continue;
  64.       }
  65.       conf_read = 1;
  66.       fclose(fp);
  67.       read_configuration(q);
  68.       return;
  69.     } else if(strcasecmp(p, "readme") == 0) {
  70.       if(readme_file) free(readme_file);
  71.       readme_file = strdup(q);
  72.     } else if(strcasecmp(p, "homedir") == 0) {
  73.       if(home_dir) free(home_dir);
  74.       home_dir = strdup(q);
  75.     } else if(strcasecmp(p, "logfile") == 0) {
  76.       if(logname) free(logname);
  77.       logname = strdup(q);
  78.     } else if(strcasecmp(p, "cachedir") == 0) {
  79.       if(dir_cache_dir) free(dir_cache_dir);
  80.       dir_cache_dir = strdup(q);
  81.     } else if(strcasecmp(p, "host") == 0) {
  82.       IPrange *nl;
  83.       nl = parse_ipline(q);
  84.       if(nl) {
  85.     if(!iptab) {
  86.       iptot = 1;
  87.       iptab = (IPrange **)malloc(sizeof(IPrange *) * iptot);
  88.     } else if(ipcnt == iptot) {
  89.       iptot *= 2;
  90.       iptab = (IPrange **)realloc((char *)iptab,sizeof(IPrange *) * iptot);
  91.     }
  92.     iptab[ipcnt++] = nl;
  93.       }
  94.     } else if(strcasecmp(p, "log") == 0) {
  95.       char *r;
  96.       int neg;
  97.       do {
  98.     /* skip to next token */
  99.     r = q; while(*r && !isspace(*r)) r++; 
  100.         if (*r) { *r++ = 0 ; while(*r && isspace(*r)) r++; }
  101.         if(strcasecmp(q, "none") == 0) {
  102.       logging = L_NONE;
  103.       break;
  104.     } else if(strcasecmp(q, "all") == 0) {
  105.       logging = L_ALL;
  106.     } else {
  107.       if(*q == '!') { neg = 1; q++;} else neg = 0;
  108.           if(strcasecmp(q, "transfers") == 0) {
  109.         log_set(L_GETFILE, neg);
  110.         log_set(L_INSTALL, neg);
  111.           } else if(strcasecmp(q, "version") == 0) log_set(L_VER, neg);
  112.       else if(strcasecmp(q, "errors") == 0) log_set(L_ERR, neg);
  113.       else if(strcasecmp(q, "getdir") == 0) log_set(L_GETDIR, neg);
  114.       else if(strcasecmp(q, "getfile") == 0) log_set(L_GETFILE, neg);
  115.       else if(strcasecmp(q, "upload") == 0) log_set(L_UPLOAD, neg);
  116.       else if(strcasecmp(q, "install") == 0) log_set(L_INSTALL, neg);
  117.       else if(strcasecmp(q, "delfile") == 0) log_set(L_DELFILE, neg);
  118.       else if(strcasecmp(q, "deldir") == 0) log_set(L_DELDIR, neg);
  119.       else if(strcasecmp(q, "setpro") == 0) log_set(L_SETPRO, neg);
  120.       else if(strcasecmp(q, "getpro") == 0) log_set(L_GETPRO, neg);
  121.       else if(strcasecmp(q, "makedir") == 0) log_set(L_MAKEDIR, neg);
  122.       else if(strcasecmp(q, "grabfile") == 0) log_set(L_GRABFILE, neg);
  123.         }
  124.     q = r;
  125.       } while (*q);
  126.     } else if(strcasecmp(p, "port") == 0)
  127.       udp_port = atoi(q);
  128.     else if(strcasecmp(p, "thruput") == 0) {
  129.       if(strcasecmp(q, "off") == 0) maxthcallowed = 0;
  130.       else maxthcallowed = atoi(q);
  131.       if(maxthcallowed < 0) maxthcallowed = 0;
  132.     } else if(strcasecmp(p, "setuid") == 0) {
  133.       if(strcasecmp(q, "off") == 0) run_uid = 0;
  134.       else run_uid = atoi(q);
  135.     } else if(strcasecmp(p, "daemonize") == 0) {
  136.       if(strcasecmp(q, "off") == 0) daemonize = 0;
  137.       else daemonize = 1;
  138.     } else if(strcasecmp(p, "debug") == 0) {
  139.       if(strcasecmp(q, "off") == 0) dbug= 0;
  140.       else dbug = 1;
  141.     } else if(strcasecmp(p, "usecachedir") == 0) {
  142.       if(strcasecmp(q, "off") == 0) always_use_cache_dir = 0;
  143.       else always_use_cache_dir = 1;
  144.     } else if(strcasecmp(p, "restricted") == 0) {
  145.       if(strcasecmp(q, "off") == 0) priv_mode = 0;
  146.       else priv_mode = 1;
  147.     } else if(strcasecmp(p, "reverse_name") == 0) {
  148.       if(strcasecmp(q, "off") == 0) no_unnamed = 0;
  149.       else no_unnamed = 1;
  150.     } else if(strcasecmp(p, "read_only") == 0) {
  151.       if(strcasecmp(q, "off") == 0) read_only = 0;
  152.       else read_only = 1;
  153.     } else {
  154.       fprintf(stderr, "Invalid command (%s) in config file, skipping.\n", p);
  155.     }
  156.   }
  157. }
  158.  
  159. static void check_required_vars PROTO0((void))
  160. {
  161.   if(!inetd_mode && !udp_port) {
  162.     fprintf(stderr, "No port set in config file. Exiting.\n");
  163.     exit(-1);
  164.   }
  165.   if(!home_dir) {
  166.     fprintf(stderr, "No home directory set in config file. Exiting.\n");
  167.     exit(-1);
  168.   }
  169.   if(!readme_file) {
  170.     readme_file = strdup(".README");
  171.   }
  172.   if(logging && !logname) {
  173.     fprintf(stderr, "No log file set in config file. Exiting.\n");
  174.     exit(-1);
  175.   }
  176.   if(always_use_cache_dir && !dir_cache_dir) {
  177.     fprintf(stderr, "No cache directory set in config file. Exiting.\n");
  178.     exit(-1);
  179.   }
  180. }
  181.  
  182. void load_configuration PROTO0((void))
  183. {
  184.   read_configuration(config_file ? config_file : CONF_FILE);
  185.   check_required_vars();
  186. }
  187.